package com.itheima.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.mapper.AddressBookMapper;
import com.itheima.mapper.OrderMapper;
import com.itheima.mapper.ShoppingCartMapper;
import com.itheima.pojo.*;
import com.itheima.service.OrderService;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;


@Log4j2
@Service
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements OrderService {


    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private AddressBookMapper addressBookMapper;
    @Autowired
    private ShoppingCartMapper shoppingCartMapper;

    /**
     * 订单分页查询
     * @param beginTime
     * @param endTime
     * @param number
     * @param page
     * @param pageSize
     * @return
     */
    @Override
    public Result getAll(String beginTime, String endTime, Integer number,
                         Integer page, Integer pageSize) {
        QueryWrapper wrapper = new QueryWrapper<>();
        if (number!=null){
        wrapper.eq("number", number);
    }
        Page page1 = orderMapper.selectPage(new Page<>(page, pageSize), wrapper);
        return new Result(Code.OK, null,page1, "{}");
    }


    /**
     * 修改订单状态
     * @param
     * @return
     */
    @Override
    public Result update(Integer status,String id) {
        Order order = new Order();
        order.setId(id);
        order.setStatus(status);
        orderMapper.updateById(order);
        return new Result(Code.OK,null,"{}");
    }


    /**
     * 移动端提交订单
     * @return
     */
    @Override
    @Transactional
    public Result placeOrder(@RequestBody Order order) {
        //查地址 手机号 收货人
        QueryWrapper<AddressBook> wrapper = new QueryWrapper<>();
        wrapper.eq("id", order.getAddressBookId());
        AddressBook one = addressBookMapper.selectOne(wrapper);
        Order order1 = new Order();
        order1.setPhone(one.getPhone());
        order1.setConsignee(one.getConsignee());
        order1.setAddress(one.getDetail());
        orderMapper.insert(order1);
        //数量 金额
        // QueryWrapper<ShoppingCart> wrapper1 = new QueryWrapper<>();
        // wrapper1.eq("id",order.getNumber());
        // ShoppingCart one1 = shoppingCartMapper.selectOne(wrapper1);
        // Order order2 = new Order();
        // order2.setAmount(one1.getNumber());
        // orderMapper.insert(order2);


        return new Result(Code.OK,null,"{}");
    }


    /**
     * 订单列表
     * @param page
     * @param pageSize
     * @return
     */
    @Override
    public Result orderList(Integer page, Integer pageSize) {

        Page page1 = orderMapper.selectPage(new Page<>(page, pageSize), null);
        return new Result(Code.OK, page1,"{}");
    }
}
